
#ifndef _BLUETOOTH_H
#define _BLUETOOTH_H
#include "typedef.h"
#include "include.h"


#define BLE_DATA_BUFFER_SIZE			20

#define BLE_TX_HANDLE_FF03				0x0006
#define BLE_SETTING_NOTIFYF3_HANDLE		0x0007			
#define BLE_RX_HANDLE_FF02				0x0009
#define BLE_RX_HANDLE_FF05				0x000b
#define BLE_RX_HANDLE_FF08				0x000d
#define BLE_SETTING_NOTIFYF8_HANDLE		0x000e


#define BLE_NAME_BUFFER_SIZE			20
#define BLE_ADDR_SIZE					6
#define BLE_ADV_SCAN_DATA_MAX_SIZE			31

#define LPM_WAKE_UP_TIMER			0
#define LPM_WAKE_UP_GPIO			2
#define POWER_ON					1
#define UI_STATE_BLE_CONNECTED          9

#define BLE_CONN_STATE_DISCONNECTED 	0
#define BLE_CONN_STATE_CONNECTED 	1

#define ADV 1
#define SCAN 2

typedef struct {
	u8_t len;
	u16_t handle;
	u8_t buff[BLE_DATA_BUFFER_SIZE];
}BleData;

typedef struct {
	u8_t wptr;
	u8_t rptr;
	u8_t wcnt;
	u8_t rcnt;
}BleDataBufferIpc;

typedef enum {
	ScanTypePassive =0,
	ScanTypeActive = 1
}BT_ScanType_t;

typedef struct {
	u8_t BleNameLen;
	u8_t BleNameBuffer[BLE_NAME_BUFFER_SIZE];
}BleName;

typedef struct {
	u8_t len;
	u8_t pdata[BLE_ADV_SCAN_DATA_MAX_SIZE];
}BleBroadcastData;


void KickWatchDog(void);

#define BT_IsBleconnected()           ( (REG16(mem_ui_state_map)  >> UI_STATE_BLE_CONNECTED) & 0x01)
#define BT_SetBDAddr(pAddr) do{xmemcpy((u8_t*)P_REG8(mem_le_lap),(u8_t __xdata *)pAddr,BLE_ADDR_SIZE);}while(0)
#define BT_AdvEnable(enable_flag) do{REG8(mem_le_adv_enable) = enable_flag;}while(0)
#define BT_BleDisconnect() do{DisconnectBle = 1;}while(0)


PUBLIC void BT_SetAdvInterval(u16_t adv_Interval);
PUBLIC void BT_SetAdvData(u8_t* p_data, u8_t len);
PUBLIC void BT_SetScanData(u8_t* p_data, u8_t len);
PUBLIC void BT_BleUpdateConnection(void);
PUBLIC void BT_BleSendData(u8_t * pbuff, u8_t len, u16_t handle);
PUBLIC bool BT_DidBleRecieveData(void);
PUBLIC void BT_BleRecieveData(BleData* pdata) ;
u8_t ChangeBleState(void);
static u8_t Ble_ModifyAdvName(u8_t*Name,u8_t NameLen,u8_t* BleData);
u8_t ChangeName(u8_t*Name,u8_t NameLen);

/*******************************************************/
/**************HCI EVENT Definitions**************/
/*******************************************************/
#define HCI_EVENT_LE_CONN_REP 		0x02
#define HCI_EVENT_LE_DIS_REP		0x05
#define HCI_EVENT_LE_DATA_REP		0x08


#endif


